import { ImageSpec } from "../consts/Assets";
import { EventNames } from "../consts/EventNames";
import EventBus from "../core/EventBus";
import ScreenManager from "../core/ScreenManager";
import Utils from "../Utils";

export default class WaitForImageAction extends bt.Action {
    private static TIMIEOUT: number = 1000 * 10;
    private startTime: number;

    public static create(imgSpec: ImageSpec, timeout: number = WaitForImageAction.TIMIEOUT): WaitForImageAction {
        return new WaitForImageAction(imgSpec, timeout);
    }

    constructor(private imgSpec: ImageSpec, private timeout: number = WaitForImageAction.TIMIEOUT) {
        super();
        this.startTime = 0;
    }

    execute(input: bt.BlackBoard): number {

        if (this.startTime == 0) {
            this.startTime = (new Date()).getTime();
        } else {
            let currTime = (new Date()).getTime();
            if (currTime - this.startTime > this.timeout) {
                this.startTime = 0;
                return bt.State.failure;
            }
        }

        if (ScreenManager.matchImage(this.imgSpec)) {
            this.startTime = 0;
            return bt.State.success;
        }

        return bt.State.running;
    }
}